﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using XRpgLibrary.Events;
using EyesOfTheDragon.Events;
using EyesOfTheDragon.Components;
using Anthem.Components;

namespace EyesOfTheDragon.Attacks
{
    public class BulletAttack : IPhysics, IDraw, IUpdate, IDestroy
    {
        Texture2D texture;
        private Vector2 position = Vector2.Zero;
        private Vector2 velocity = Vector2.Zero;
        private float lifeTime = 2;
        
        private bool enabled = false;
        public bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }
        public BulletAttack(Game gameRef)
        {
            texture = gameRef.Content.Load<Texture2D>(@"Effects\knife");
        }

        public void init()
        {
            PhysicsSimulation.Instance.add(this);
            DrawList.Instance.add(this);
            UpdateList.Instance.add(this);
            enabled = true;
        }
        public void destroy()
        {
            PhysicsSimulation.Instance.remove(this);
            DrawList.Instance.remove(this);
            UpdateList.Instance.remove(this);
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        public void Draw(GameTime gt, SpriteBatch sp)
        {
            float rot = 0;
            if (velocity.Y < 0)
                rot = (float)Math.PI/2;
            else if (velocity.Y >0)
                rot = (float)Math.PI + (float)Math.PI/2;
            else if (velocity.X >0)
                rot = (float)Math.PI;
            else 
                rot = 0;
            Rectangle ourRect = new Rectangle((int)position.X, (int)Position.Y, texture.Width , texture.Height );
            sp.Draw(texture, ourRect, new Rectangle(0,0, texture.Width, texture.Height), Color.White,rot,new Vector2(texture.Width , texture.Height) ,SpriteEffects.None, 0);
        }
        public void Update(GameTime gt)
        {
            
            Rectangle ourRect = new Rectangle((int)position.X, (int)Position.Y, texture.Width, texture.Height);
            foreach (MonsterSprite ms in Monster.Instance.Monsters)
            {
                if (!ms.Enabled)
                    continue;
                bool collision = false;
                if (ourRect.Intersects(ms.getRect))
                {
                   
                   // ms.Enabled = false;
                   // DestroyList.Instance.add(ms);
                    if ((ms as FinalBoss) == null)
                        ms.Kill();
                    else
                        (ms as FinalBoss).Damage(10, false);
                    DestroyList.Instance.add(this);
                    return;
                }
            }
            lifeTime -= gt.ElapsedGameTime.Milliseconds / 1000f;
            if (lifeTime < 0)
            {
                Enabled = false;
                DestroyList.Instance.add(this);
            }
        }
    }
}
